"""
    Controller for classroom table.
"""

from turbogears import validators, widgets
from mnemoply import model
from mnemoply.controllers.lookup import LookupController, TableCol, TableLookupDataSource
from mnemoply.controllers.crud import CRUDController
from mnemoply.controllers import Report
from mnemoply.utils import objs2ids
from mnemoply.mass_actions import MassAction, MassActionForm

class MATimetable(MassAction):
    """
        Print timetable for selected classrooms.
    """

    target_class = model.Classroom
    name = "timetable"
    title = _("Timetable")
    read_only = True
    show_names_options = [
        _("Show group title?"),
        _("Show plan title?"),
        _("Show trend title?"),
    ]

    form = MassActionForm(name=name,
        fields=[
            widgets.CalendarDatePicker(
                label = _('Date'),
                name = 'date',
                calendar_lang = 'ru',
                format = '%d.%m.%Y',
                validator = validators.DateTimeConverter(format="%d.%m.%Y")),
            widgets.CheckBox(
                name = "show_teacher",
                default = True,
                label = _("Show teacher?"),
            ),
            widgets.CheckBox(
                name = "show_dates",
                default = False,
                label = _("Show group dates?"),
            ),
            widgets.CheckBox(
                name = "show_time",
                default = True,
                label = _("Show group time?"),
            ),
            widgets.CheckBox(
                name = "show_place",
                default = True,
                label = _("Show free places?"),
            ),
            widgets.RadioButtonList(
                name = 'show_names',
                options = map(None, xrange(1, len(show_names_options) + 1), show_names_options),
                default = 1,
                label = _("Which names to show?")
            ),
            widgets.CheckBox(
                name = "show_unused",
                default = False,
                label = _("Show unused?"),
            ),
        ]
    )

    def call(self, objs, date, show_teacher, show_dates, show_time, show_place, show_names, show_unused):
        return Report.generate(
            'Timetable',
            ids = objs2ids(objs),
            timetable_date = date.date(), # cast datetime.datetime to datetime.date
            show_teacher = show_teacher,
            show_dates = show_dates,
            show_time = show_time,
            show_place = show_place,
            show_names = show_names,
            show_unused = show_unused,
        )

class ClassroomController(LookupController, CRUDController):
    model_object = model.Classroom
    lookup_columns = [
        TableCol(name='office', caption=_('Office')),
        TableCol(name='roomNumber', caption=_('Room #')),
        TableCol(name='ctype', caption=_('Type')),
    ]
    view_fields = ['officeID', 'roomNumber', 'classroomTypeID']

    def __init__(self):
        LookupController.__init__(self,
            data_source=TableLookupDataSource('classroom_lookup'),
            columns=self.lookup_columns,
            mass_actions_class=self.model_object,
            title=self.model_object.model_title,
        )
        CRUDController.__init__(self, model_object=self.model_object)

exposed = ClassroomController()
